package day5;

import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.*;
import java.util.Arrays;
import java.util.Date;

public class Homework {
    private String name;
    public int value;
    protected Date time;
    private Homework(String s){}
    public void test(){ }
    public int test(int a){return a;}

    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, IOException {
        //demo1();
        //demo2();
        demo3();
        //demo4();
    }
    public static void demo1() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        //获取class
        Class c=Class.forName("day5.Homework");
        Constructor con=c.getDeclaredConstructor(String.class);
        con.setAccessible(true);
        Homework h=(Homework) con.newInstance("aaa");
    }
    public static void demo2(){
        Object ary=Array.newInstance(String.class,5);
        Array.set(ary,0,"A");
        Array.set(ary,1,"B");
        Array.set(ary,2,"C");
        for(int i=0;i<Array.getLength(ary);i++){
            System.out.println(Array.get(ary,i));
        }
    }
    public static void demo3(){
        Homework h=new Homework("111");
        set(h,"name","李四");
        set(h,"time",new Date());
        System.out.println(get(h,"name"));
        System.out.println(get(h,"time"));
    }
    public static void set(Object obj,String name,Object value){
        try {
            Class c=obj.getClass();
            Field f=c.getDeclaredField(name);
            f.setAccessible(true);
            f.set(obj,value);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    public static Object get(Object obj,String name){
        Object value=null;
        try {
            Class c=obj.getClass();
            Field f=c.getDeclaredField(name);
            f.setAccessible(true);
            f.get(obj);
            value=f.get(obj);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return value;
    }
    public static void demo4() throws IOException {
        Class c=Homework.class;
        Field[] fs=c.getDeclaredFields();
        Method[] ms=c.getDeclaredMethods();
        Constructor[] con=c.getDeclaredConstructors();
        StringBuilder sb=new StringBuilder();
        sb.append("方法有:");
        for(Method m:ms){
            String name=m.getName();
            Class[] classes=m.getParameterTypes();
            sb.append(name+" "+ Arrays.toString(classes));

        }
        sb.append("\n属性有：");
        for(Field f:fs){
            String name=f.getName();
            Class class1=f.getType();
            sb.append(class1+" "+name+" ");

        }
        sb.append("\n构造有：");

        for(Constructor con1:con){
            String name=con1.getName();
            sb.append(name+"");
        }
        System.out.println(sb);
        FileWriter fw=new FileWriter("d://io//result.txt");
        fw.write(sb.toString());
        fw.close();;
    }
}
